package com.zxkj.commonpatrol.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.LoadingDialog;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.MProfileAdapt;
import com.zxkj.commonpatrol.databinding.ActivityMProfileBinding;
import com.zxkj.commonpatrol.model.response.RouteCustomInfoResponse;
import com.zxkj.commonpatrol.viewmodel.MProfileViewModel;

import java.util.List;

public class MProfileActivity extends BaseVmDataBindingActivity<ActivityMProfileBinding, MProfileViewModel> {

    private String routeInspectNo;
    private String routeTypeCode;
    private MiniLoadingDialog loadingDialog;
    private String Name;
    private MProfileAdapt adapt;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_m_profile;
    }

    @Override
    public int initVariableId() {
        return BR.MProfileViewmodel;
    }



    @Override
    protected void initUI() {

         routeInspectNo = getIntent().getStringExtra("RouteInspectNo");
        routeTypeCode = getIntent().getStringExtra("RouteTypeCode");
        Name = getIntent().getStringExtra("Name");
         binding.tvTitle.setText(Name);
            binding.recy.setLayoutManager(new LinearLayoutManager(this));
            initDatas();
    }

    private void initDatas() {
        if (loadingDialog==null){
            loadingDialog= new MiniLoadingDialog(this, "加載中...");
        }

        loadingDialog.show();
        viewModel.getData(routeInspectNo,routeTypeCode);
    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.btnNo.setOnClickListener(v -> {

            if (adapt!=null){
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();

                    viewModel.SaveData(routeInspectNo,adapt.Values);



            }


        });
        binding.qkBack.setOnClickListener(v->{
            ActivityTool.getInstance().popOneActivity(MProfileActivity.this);
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.save.observe(this, new Observer<StateData<Object>>() {
            @Override
            public void onChanged(StateData<Object> objectStateData) {
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
                 if (objectStateData.getStatus()==StateData.DataStatus.SUCCESS){
                     XToast.success(MProfileActivity.this,"保存成功").show();
                     ActivityTool.getInstance().popOneActivity(MProfileActivity.this);
                 }else{
                     XToast.error(MProfileActivity.this,"保存失敗").show();
                 }
            }
        });

        viewModel.data.observe(this, new Observer<StateData<Object>>() {
            @Override
            public void onChanged(StateData<Object> objectStateData) {
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }

                if (objectStateData.getStatus()==StateData.DataStatus.SUCCESS){
                    Object data = objectStateData.getData();
                    List<RouteCustomInfoResponse> routeCustomInfoResponse =(List<RouteCustomInfoResponse>)data;
                     adapt =new MProfileAdapt(R.layout.item_mprofile_nomal,routeCustomInfoResponse);
                    binding.recy.setAdapter(adapt);
                    XToast.success(MProfileActivity.this,"加載數據成功").show();
                }else{
                    XToast.success(MProfileActivity.this,"加載數據失敗").show();
                }
            }
        });
    }
}
